package com.d2.pay.rest;

import com.d2.pay.utils.Helper;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Auth: Alexander Lo
 * Date: 2020-03-23
 * Description:
 */
@RestController
public class NotifyController {
    /**
 * 支付异步回调接口
 * 用于接收绿点的异步通知信息
 * 在这里执行你自己的订单支付成功逻辑，比如加积分，延长用户服务时间等...'
 * @param req
 * @param rep
 * @throws IOException
 */
@PostMapping(value = "notify.do")
public Map<String, Object> notify(HttpServletRequest req, HttpServletResponse rep) {

    String merchToken = "123"; // 此处填写商户的 token
    String greenpay_id = req.getParameter("greenpay_id");
    String ordernum = req.getParameter("ordernum");
    String account = req.getParameter("account");
    String amount = req.getParameter("amount");
    String realamount = req.getParameter("realamount");
    String type = req.getParameter("type");
    String orderuid = req.getParameter("orderuid");
    String key = req.getParameter("key");

    String orderStr = Helper.md5(ordernum + orderuid + greenpay_id + amount + realamount + type + merchToken);

    if(orderStr.equals(key)) {
        // TODO: 支付成功：执行你自己的订单支付成功逻辑，比如加积分，延长用户服务时间等...
    } else {
        // TODO: 支付失败：加密串不匹配
    }

    Map<String, Object> result = new HashMap<String, Object>();
    result.put("code", 200);
    result.put("content", "ok");
    return result;
}


}
